package com.pk.platform.business.core.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.pk.platform.business.core.service.IListViewService;
import com.pk.platform.business.core.vo.ListViewVO;
import com.pk.platform.domain.list.ListDefine;
import com.pk.platform.domain.list.ListField;
import com.pk.platform.domain.list.ListModule;
import com.pk.platform.util.DateConverter;
import com.pk.platform.util.SelectOption;
import com.pk.platform.util.StringConverter;
import com.pk.platform.util.WebUtil;

public class ListViewAction extends GenericAction {

	private IListViewService listViewService;
	
	private ListModule lm = new ListModule();
	
	private ListDefine ld = new ListDefine();
	
	private ListField lf = new ListField();
	
	private String[] jqg_listGrid;
	
	private Integer[] fieldSort;
	
	private ListViewVO lvvo = new ListViewVO();
	
	private List<SelectOption> dateFormatList = new ArrayList<SelectOption>();
	
	private Map<String, Object> gridObj = new HashMap<String, Object>();
	
	private String formatterScript;
	
	public String toSaveListModule()
	{
		if(StringConverter.isNotNull(lm.getId()))
		{
			return "updatelistmodule";
		}
		return "addlistmodule";
	}
	
	public String openListModule()
	{
		lm = listViewService.queryListModuleById(lm.getId());
		return SUCCESS;
	}
	
	public String toSaveListDefine()
	{
		if(StringConverter.isNotNull(ld.getId()))
		{
			ld = listViewService.queryListDefineById(ld.getId());
			String realCode = ld.getListCode();
			int index = realCode.lastIndexOf("_");
			ld.setListCode(realCode.substring(index + 1, realCode.length()));
			ld.setSqlStr(StringConverter.blobToString(ld.getSqlScript()));
		}
		else
		{
			ld.setPageSize(15);
		}
		return SUCCESS;
	}
	
	public String toSaveListField()
	{
		dateFormatList = WebUtil.constructOptions(DateConverter.DATE_FORMAT);
		if(StringConverter.isNotNull(lf.getId()))
		{
			lf = listViewService.queryListFieldById(lf.getId());
		}
		return SUCCESS;
	}
	
	public String saveListModule()
	{
		message = listViewService.saveListModule(lm);
		return SUCCESS;
	}
	
	public String saveListDefine()
	{
		message = listViewService.saveListDefine(ld);
		return SUCCESS;
	}
	
	public String saveListField()
	{
		listViewService.saveListField(lf);
		return SUCCESS;
	}
	
	public String delListModule()
	{
		message = listViewService.delListModule(lm.getId());
		return SUCCESS;
	}
	
	public String delListDefine()
	{
		listViewService.delListDefine(ld.getId());
		return SUCCESS;
	}
	
	public String delListField()
	{
		if(jqg_listGrid != null)
		{
			listViewService.delListField(jqg_listGrid);
		}
		return SUCCESS;
	}
	
	public String listModuleManage()
	{
		return SUCCESS;
	}
	
	public String listDefineManage()
	{
		lm = listViewService.queryListModuleById(lvvo.getModuleId());
		return SUCCESS;
	}
	
	public String listModuleManageList()
	{
		list = listViewService.queryListModule(pager);
		return SUCCESS;
	}
	
	public String listDefineManageList()
	{
		list = listViewService.queryListDefineByModule(lvvo.getModuleId(), pager);
		return SUCCESS;
	}
	
	public String listFieldManageList()
	{
		list = listViewService.queryListFieldByDefine(lvvo.getListId());
		return SUCCESS;
	}
	
	public String batchSaveSort()
	{
		if(jqg_listGrid != null)
		{
			listViewService.changeListFieldSort(jqg_listGrid, fieldSort);
		}
		return SUCCESS;
	}
	
	public String loadGridData()
	{
		HttpServletRequest request = this.getRequest();
		Map<String, Object> parameters = request.getParameterMap();
		for(String key : parameters.keySet())
		{
			String[] params = (String[]) parameters.get(key);
			StringBuffer buffer = new StringBuffer();
			for (String param : params) {
				buffer.append("," + param);
			}
			String paramValue = buffer.toString();
			if(paramValue.length() > 0)
			{
				paramValue = paramValue.substring(1);
			}
			paramObj.put(key, paramValue);
		}
		list = listViewService.loadGridData(lvvo.getListCode(), paramObj, pager);
		return SUCCESS;
	}
	
	
	

	public IListViewService getListViewService() {
		return listViewService;
	}

	public void setListViewService(IListViewService listViewService) {
		this.listViewService = listViewService;
	}

	public ListModule getLm() {
		return lm;
	}

	public void setLm(ListModule lm) {
		this.lm = lm;
	}

	public ListDefine getLd() {
		return ld;
	}

	public void setLd(ListDefine ld) {
		this.ld = ld;
	}

	public ListField getLf() {
		return lf;
	}

	public void setLf(ListField lf) {
		this.lf = lf;
	}

	public String[] getJqg_listGrid() {
		return jqg_listGrid;
	}

	public void setJqg_listGrid(String[] jqgListGrid) {
		jqg_listGrid = jqgListGrid;
	}

	public Integer[] getFieldSort() {
		return fieldSort;
	}

	public void setFieldSort(Integer[] fieldSort) {
		this.fieldSort = fieldSort;
	}

	public ListViewVO getLvvo() {
		return lvvo;
	}

	public void setLvvo(ListViewVO lvvo) {
		this.lvvo = lvvo;
	}

	public List<SelectOption> getDateFormatList() {
		return dateFormatList;
	}

	public void setDateFormatList(List<SelectOption> dateFormatList) {
		this.dateFormatList = dateFormatList;
	}

	public Map<String, Object> getGridObj() {
		return gridObj;
	}

	public void setGridObj(Map<String, Object> gridObj) {
		this.gridObj = gridObj;
	}

	public String getFormatterScript() {
		return formatterScript;
	}

	public void setFormatterScript(String formatterScript) {
		this.formatterScript = formatterScript;
	}
	
}
